package com.czy.seed.mvc.sys.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.czy.seed.mvc.base.service.impl.BaseServiceImpl;
import com.czy.seed.mvc.home.entity.AddressManager;
import com.czy.seed.mvc.home.entity.BeginningBudget;
import com.czy.seed.mvc.home.mapper.AddressManagerMapper;
import com.czy.seed.mvc.home.mapper.BeginningBudgetMapper;
import com.czy.seed.mvc.sys.entity.SysUser;
import com.czy.seed.mvc.sys.entity.SysUserRole;
import com.czy.seed.mvc.sys.entity.SysUserVo;
import com.czy.seed.mvc.sys.mapper.SysUserMapper;
import com.czy.seed.mvc.sys.mapper.SysUserRoleMapper;
import com.czy.seed.mvc.sys.service.SysUserService;
import com.czy.seed.mvc.util.PrincipalUtil;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class SysUserServiceImpl extends BaseServiceImpl<SysUser> implements SysUserService {
    public static Logger logger = LoggerFactory.getLogger(SysUser.class);

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private BeginningBudgetMapper beginningBudgetMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private AddressManagerMapper addressManagerMapper;

    @Override
    @Transactional
    public Boolean addUserUnit(HttpServletRequest request) {
        try {
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            String user = request.getParameter("user");
            //装换为临时的实体对象信息
            SysUserVo sysUser = JSONObject.parseObject(user, SysUserVo.class);
            //初始化角色信息
            SysUserRole sur = new SysUserRole();
            //初始化用户信息
            SysUser surs = new SysUser();
            surs.setName(sysUser.getName());
            surs.setUsername(sysUser.getUsername());
            surs.setSex(sysUser.getSex());
            surs.setTelephone(sysUser.getTelephone());
            surs.setEmail(sysUser.getEmail());
            surs.setXmname(sysUser.getXmname());
            surs.setAddress(sysUser.getAddress());
            String encodePassword = passwordEncoder.encode("000000");
            surs.setPassword(encodePassword);
            if (sysUser.getCreateBy() == null || "".equals(sysUser.getCreateBy())) {
                surs.setCreateBy(PrincipalUtil.getLoginUser().getId());
            }else{
                surs.setCreateBy(sysUser.getCreateBy());
            }
            surs.setCreateDt(new Date());
            surs.setUpdateDt(new Date());
            int i = sysUserMapper.insert(surs);
            //初始化年初预算信息
            BeginningBudget beginningBudget = new BeginningBudget();
            beginningBudget.setBalanceAmount(sysUser.getBalanceAmount());
            beginningBudget.setBudgetAmount(sysUser.getBudgetAmount());
            beginningBudget.setCreationBy(surs.getId());
            beginningBudget.setCreationTime(new Date());
            beginningBudget.setRemainBudget(sysUser.getBalanceAmount().add(sysUser.getBudgetAmount()));
            beginningBudget.setUserId(surs.getId());
            beginningBudgetMapper.insert(beginningBudget);
            //初始化收货地址信息
            AddressManager addressManager = new AddressManager();
            //采购单位
            addressManager.setUserType(2);
            addressManager.setIscheck(true);
            addressManager.setCreateBy(surs.getId());
            addressManager.setCreateTime(new Date());
            addressManager.setAddress(sysUser.getAddress());
            addressManager.setUserId(surs.getId());
            addressManagerMapper.insert(addressManager);
            sur.setSysUserId(surs.getId());
            sur.setSysRoleId(13L);
            int y = sysUserRoleMapper.insert(sur);
            return true;
        } catch (Exception e) {
            logger.error("错误信息为:"+e);
            return false;
        }


    }

    @Override
    public List<SysUser> presentUser(Long userId, String type) {
        return sysUserMapper.presentUser(userId, type);
    }

}
